package com.manli.api.service;

import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.UserLinkUnionidDao;
import com.manli.api.dto.UserLinkUnionid;


@Service("userLinkUnionidService")
public class UserLinkUnionidService {
    @Autowired
    UserLinkUnionidDao userLinkUnionidDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParams params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from userLinkUnionid where 1=1 ");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<UserLinkUnionid> pageQuery = new PageQuery<UserLinkUnionid>(params.getPage(),params.getLimit());
        PageQuery<UserLinkUnionid> query =sqlManager.execute(sqlReady,UserLinkUnionid.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<UserLinkUnionid> list = userLinkUnionidDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer userLinkUnionidId) {
        UserLinkUnionid userLinkUnionid = userLinkUnionidDao.single(userLinkUnionidId);
        return ResultUtil.success(userLinkUnionid);
    }

    @Transactional
    public Result save(UserLinkUnionid userLinkUnionid) {
        userLinkUnionidDao.insert(userLinkUnionid);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(UserLinkUnionid userLinkUnionid) {
        userLinkUnionidDao.updateTemplateById(userLinkUnionid);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(UserLinkUnionid userLinkUnionid) {
        userLinkUnionidDao.updateTemplateById(userLinkUnionid);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            userLinkUnionidDao.deleteById(id);
        }
        return ResultUtil.success();
    }


    public List<UserLinkUnionid> getByUnionid(String unionid, int status) {
        return userLinkUnionidDao.createLambdaQuery().andEq("unionid",unionid)
                .andEq("status",status).select();
    }

    public UserLinkUnionid getSingleByUnionid(String unionid , int status) {
        return userLinkUnionidDao.createLambdaQuery().andEq("unionid",unionid).andEq("status",status).single();
    }

    public UserLinkUnionid getByUserId(Integer userId , int status) {
        return userLinkUnionidDao.createLambdaQuery().andEq("user_id",userId).andEq("status",status).single();
    }
}
